package com.ultrawise.survey.common.utils;

import java.io.*;
import java.net.*;
import java.sql.*;
import java.text.*;
import javax.servlet.http.*;

/**
 * User: Administrator Date: 2005-9-1 Time: 15:30:55 To change this template use File | Settings | File Templates.
 */
public class HtmlTools
{
	/**
	 * 将字符串替换为Html格式的
	 * 
	 * @param input
	 * @return
	 */
	public static String toHtml(String input)
	{
		StringBuffer buf = new StringBuffer();
		if (input == null)
		{
			return "";
		}
		for (int i = 0; i < input.length(); i++)
		{
			switch (input.charAt(i))
			{
			case '\"': // 转换双引号
				buf.append("&quot;");
				break;
			case '\'': // 转换单引号
				buf.append("&acute;");
				break;
			case ' ': // 转换空格
				buf.append("&nbsp;");
				break;
			case '<': // 转换小于符号
				buf.append("&lt;");
				break;
			case '>': // 转换大于符号
				buf.append("&gt;");
				break;
			case '\n': // 转换回车
				buf.append("<br>");
				break;
			default:
				buf.append(input.charAt(i));
			}
		}
		return buf.toString();
	}

	/**
	 * 打开一个消息框
	 * 
	 * @param strMsg
	 * @param response
	 * @throws IOException
	 */
	public static void MessageBox(String strMsg, HttpServletResponse response) throws IOException
	{
		PrintWriter out = response.getWriter();
		out.println("<script language='javascript'>");
		out.println("window.alert('" + strMsg + "');");
		out.println("</script>");
	}

	/**
	 * 转向一个指定的页面
	 * 
	 * @param strURL
	 * @param response
	 * @throws IOException
	 */
	public static void redirectToURL(String strURL, HttpServletResponse response) throws IOException
	{
		PrintWriter out = response.getWriter();
		out.println("<script language='javascript'>");
		out.println("window.location.href='" + strURL + "';");
		out.println("</script>");
	}

	/**
	 * 关闭一个窗口
	 * 
	 * @param response
	 * @throws IOException
	 */
	public static void WindowClose(HttpServletResponse response) throws IOException
	{
		PrintWriter out = response.getWriter();
		out.println("<script language='javascript'>");
		out.println("window.close();");
		out.println("</script>");
	}

	/**
	 * 得到当前时间的字符串
	 * 
	 * @return
	 */
	public static String getNow()
	{
		java.util.Date date = new java.util.Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:dd");
		String strNow = sdf.format(date);
		return strNow;
	}

	/**
	 * 得到长日期格式
	 * 
	 * @param date
	 * @return
	 */
	public static String getFormatDate(java.util.Date date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:dd");
		String strFormatDate = sdf.format(date);
		return strFormatDate;
	}

	/**
	 * 翻页
	 * 
	 * @param form
	 *            String
	 * @param intRecordCount
	 *            int
	 * @param intPageSize
	 *            int
	 * @param intPage
	 *            int
	 * @return String
	 */
	public static String turnPage(String form, int intRecordCount, int intPageSize, int intPage)
	{
		int intPageCount = ((intRecordCount % intPageSize) == 0) ? intRecordCount / intPageSize : intRecordCount
				/ intPageSize + 1;
		// PrintWriter out = response.getWriter();
		String strPage = "每页 <input type=\"text\" name=\"ps\" value=\""
				+ intPageSize
				+ "\" style=\"width:20px;height:14px;text-align:center;border-top:0px;border-left:0px;border-right:0px;border-bottom:1px solid #000000;\"> 条 <img src=\"/Manage/images/refresh.gif\" alt=\"刷新\" onClick=\"javascript:listForm.submit();\" style=\"cursor:hand;\">&nbsp;&nbsp; 共 "
				+ intRecordCount + " 条/" + intPageCount + " 页 第 " + intPage + " 页 ";
		if (intPage > 1)
		{
			strPage += "<a href='javascript:gotoPage(1);'>首 页</a> <a href='javascript:gotoPage(" + (intPage - 1)
					+ ");'>上一页</a> ";
		} else
		{
			strPage += "<font color='#CCCCCC'>首 页 上一页</font> ";
		}
		if (intPage < intPageCount)
		{
			strPage += "<a href='javascript:gotoPage(" + (intPage + 1) + ");'>下一页</a> <a href='javascript:gotoPage("
					+ intPageCount + ");'>末 页</a>　";
		} else
		{
			strPage += "<font color='#CCCCCC'>下一页 末 页</font> ";
		}

		strPage += "跳转到：<select name='sepage' onChange='var pageNum=this.options[this.selectedIndex].value;gotoPage(pageNum);'>";
		for (int i = 1; i <= intPageCount; i++)
		{
			if (i == intPage)
			{
				strPage += "<option value='" + i + "' selected>" + i + "</option>";
			} else
			{
				strPage += "<option value='" + i + "'>" + i + "</option>";
			}
		}
		strPage += "</select>";

		strPage += "<script>";
		strPage += "function gotoPage(p){";
		strPage += form + ".value=p;";
		strPage += form.substring(0, form.lastIndexOf(".")) + ".submit();";
		strPage += "}";
		strPage += "</script>";
		// System.out.println("Text = "+strPage);
		return strPage;
		// out.println(strPage);
	}

	/**
	 * 翻页
	 * 
	 * @param form
	 *            String
	 * @param intRecordCount
	 *            int
	 * @param intPageSize
	 *            int
	 * @param intPage
	 *            int
	 * @return String
	 */
	public static String gotoPage(String form, int intRecordCount, int intPageSize, int intPage)
	{
		int intPageCount = ((intRecordCount % intPageSize) == 0) ? intRecordCount / intPageSize : intRecordCount
				/ intPageSize + 1;
		// PrintWriter out = response.getWriter();
		String strPage = "每页 " + intPageSize + " 条 共 " + intRecordCount + " 条/" + intPageCount + " 页 第 " + intPage
				+ " 页 ";
		if (intPage > 1)
		{
			strPage += "<a href='javascript:gotoPage(1);'>首 页</a> <a href='javascript:gotoPage(" + (intPage - 1)
					+ ");'>上一页</a> ";
		} else
		{
			strPage += "<font color='#CCCCCC'>首 页 上一页</font> ";
		}
		if (intPage < intPageCount)
		{
			strPage += "<a href='javascript:gotoPage(" + (intPage + 1) + ");'>下一页</a> <a href='javascript:gotoPage("
					+ intPageCount + ");'>末 页</a>　";
		} else
		{
			strPage += "<font color='#CCCCCC'>下一页 末 页</font> ";
		}

		strPage += "跳转到：<select name='sepage' onChange='var pageNum=this.options[this.selectedIndex].value;gotoPage(pageNum);'>";
		for (int i = 1; i <= intPageCount; i++)
		{
			if (i == intPage)
			{
				strPage += "<option value='" + i + "' selected>" + i + "</option>";
			} else
			{
				strPage += "<option value='" + i + "'>" + i + "</option>";
			}
		}
		strPage += "</select>";

		strPage += "<script>";
		strPage += "function gotoPage(p){";
		strPage += form + ".value=p;";
		strPage += form.substring(0, form.lastIndexOf(".")) + ".submit();";
		strPage += "}";
		strPage += "</script>";
		// System.out.println("Text = "+strPage);
		return strPage;
		// out.println(strPage);
	}

	/**
	 * 英文翻页
	 * 
	 * @param form
	 * @param intRecordCount
	 * @param intPageSize
	 * @param intPage
	 * @return
	 */
	public static String gotoPageForEnglish(String form, int intRecordCount, int intPageSize, int intPage)
	{
		int intPageCount = ((intRecordCount % intPageSize) == 0) ? intRecordCount / intPageSize : intRecordCount
				/ intPageSize + 1;
		// PrintWriter out = response.getWriter();
		String strPage = "Total " + intRecordCount + " " + intPage + "/" + intPageCount + " Page　";
		if (intPage > 1)
		{
			strPage += "<a href='javascript:gotoPage(1);'>First</a>　<a href='javascript:gotoPage(" + (intPage - 1)
					+ ");'>Previous</a>　";
		} else
		{
			strPage += "<font color='#CCCCCC'>First　Previous　</font>";
		}
		if (intPage < intPageCount)
		{
			strPage += "<a href='javascript:gotoPage(" + (intPage + 1) + ");'>Next</a>　<a href='javascript:gotoPage("
					+ intPageCount + ");'>Last</a>　";
		} else
		{
			strPage += "<font color='#CCCCCC'>Next　Last　</font>";
		}

		strPage += "Goto：";
		strPage += "<select name='sepage' onChange='var pageNum=this.options[this.selectedIndex].value;gotoPage(pageNum);'>";
		for (int i = 1; i <= intPageCount; i++)
		{
			if (i == intPage)
			{
				strPage += "<option value='" + i + "' selected>" + i + "</option>";
			} else
			{
				strPage += "<option value='" + i + "'>" + i + "</option>";
			}
		}
		strPage += "</select>";

		strPage += "<script>";
		strPage += "function gotoPage(p){";
		strPage += form + ".value=p;";
		strPage += form.substring(0, form.lastIndexOf(".")) + ".submit();";
		strPage += "}";
		strPage += "</script>";
		// System.out.println("Text = "+strPage);
		return strPage;
		// out.println(strPage);
	}

	/**
	 * 法文翻页
	 * 
	 * @param form
	 *            String
	 * @param intRecordCount
	 *            int
	 * @param intPageSize
	 *            int
	 * @param intPage
	 *            int
	 * @return String
	 */
	public static String gotoPageForFrance(String form, int intRecordCount, int intPageSize, int intPage)
	{
		int intPageCount = ((intRecordCount % intPageSize) == 0) ? intRecordCount / intPageSize : intRecordCount
				/ intPageSize + 1;
		// PrintWriter out = response.getWriter();
		String strPage = "Total " + intRecordCount + " Note et " + intPageSize;
		strPage += " de ";
		strPage += "<select name='sepage' onChange='var pageNum=this.options[this.selectedIndex].value;gotoPage(pageNum);'>";
		for (int i = 1; i <= intPageCount; i++)
		{
			if (i == intPage)
			{
				strPage += "<option value='" + i + "' selected>" + i + "</option>";
			} else
			{
				strPage += "<option value='" + i + "'>" + i + "</option>";
			}
		}
		strPage += "</select>";
		strPage += " pages à la page&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		if (intPage > 1)
		{
			strPage += "  <a href='javascript:gotoPage(1);'>Premier</a>　<a href='javascript:gotoPage(" + (intPage - 1)
					+ ");'>Précédent</a>　";
		} else
		{
			strPage += "  <font color='#CCCCCC'>Premier　Précédent　</font>";
		}
		if (intPage < intPageCount)
		{
			strPage += "<a href='javascript:gotoPage(" + (intPage + 1)
					+ ");'>Ensuite</a>　<a href='javascript:gotoPage(" + intPageCount + ");'>Dernier</a>　";
		} else
		{
			strPage += "<font color='#CCCCCC'>Ensuite　Dernier　</font>";
		}
		strPage += "<script>";
		strPage += "function gotoPage(p){";
		strPage += form + ".value=p;";
		strPage += form.substring(0, form.lastIndexOf(".")) + ".submit();";
		strPage += "}";
		strPage += "</script>";
		// System.out.println("Text = "+strPage);
		return strPage;
		// out.println(strPage);
	}

	/**
	 * 通过Socket取一个网页内容
	 * 
	 * @param strServer
	 *            String
	 * @param strPage
	 *            String
	 * @param strCharset
	 *            String
	 * @return String
	 */
	public static String getPageContent(String strServer, int intPort, String strPage, String strCharset)
	{
		String strPageContent = "";
		Socket socket = null;
		BufferedWriter out = null;
		BufferedReader in = null;
		try
		{
			// InetAddress addr = InetAddress.getByName(strServer);
			socket = new Socket(strServer, intPort); // 建立一个Socket
			socket.setSoTimeout(120000);
			// 发送命令
			out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), strCharset));
			out.write("GET " + strPage + " HTTP/1.0\r\n");
			out.write("HOST:" + strServer + "\r\n");
			out.write("\r\n");
			out.flush();
			// 接收返回的结果
			in = new BufferedReader(new InputStreamReader(socket.getInputStream(), strCharset));
			for (int i = 1; i <= 9; i++)
			{
				in.readLine();
			}
			if (in.ready())
			{
				while (true)
				{
					String strLine = in.readLine();
					if (strLine != null)
					{
						strPageContent += strLine + "\r\n";
					} else
					{
						break;
					}
				}
			}
		} catch (Exception e)
		{
			strPageContent = "";
			e.printStackTrace();
		} finally
		{
			try
			{
				out.close();
			} catch (Exception e)
			{
				e.printStackTrace();
			}
			try
			{
				in.close();
			} catch (Exception e)
			{
				e.printStackTrace();
			}
			try
			{
				socket.close();
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		// System.out.println(strPageContent);
		return strPageContent;
	}

}
